<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
/**
 * 公用基础控制器
 */
class BaseController extends Controller {

	protected $member;

	protected function _initialize() {
		parent::_initialize();
		$request= Request::instance();
		$controller_name=$request->controller();
		$action=$request->action();
		$this->assign("action",$action);
		$redirect='https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$openid='otx0ewIY-HKwa5p0gql3bDfp562A';//session("openid");//'otx0ewIY-HKwa5p0gql3bDfp562A';//
		$user_info=session('user_info');
		$cut_action = request()->action();
		if($cut_action=="wechat_notify"||$cut_action=="guanzhu"){
			return true;
		}
		$user_info = db('member') -> where("open_id", $openid) -> find();
		if(!$openid||!$user_info){
			$auth_code=input("code");
			if(!$auth_code){
				session("redirect",$redirect);
				//$url ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('wxpay_appid').'&redirect_uri='.urlencode($redirect).'&response_type=code&scope=snsapi_userinfo&state=STATE%23wechat_redirect&connect_redirect=1#wechat_redirect';
				$url ='https://llp.zh66pai.com/prox.php?appid='.config('wxpay_appid').'&redirect_uri='.urlencode($redirect).'&response_type=code&scope=snsapi_userinfo&state=STATE%23wechat_redirect&connect_redirect=1#wechat_redirect';
				//$url ='https://s.lxtx001.com/prox.php?appid='.config('wxpay_appid').'&redirect_uri='.urlencode($redirect).'&response_type=code&scope=snsapi_userinfo&state=STATE%23wechat_redirect&connect_redirect=1#wechat_redirect';
				Header("Location: $url");
				exit;
			}
			$openid=user_wx_id($auth_code);
			$user_info = db('member') -> where("open_id", $openid) -> find();
			if($user_info) {
				session('user_info', $user_info);
			} else {
				$this->add_user($openid);
			}
			session("openid",$openid);
			$from=strpos($redirect, '?from');
			$from1=strpos($redirect, '&from');
			$code=strpos($redirect, '?code');
			$code1=strpos($redirect, '&code');
			if($from){
				$redirect=substr($redirect,0,$from);
			}elseif($from1){
				$redirect=substr($redirect,0,$from1);
			}elseif($code){
				$redirect=substr($redirect,0,$code);
			}elseif($code1){
				$redirect=substr($redirect,0,$code1);
			}
			$this -> redirect($redirect);
		}else{
			$is_gz=session("is_gz");
			if(!$is_gz){
				//$this->up_user($openid);
			}
//			$user_info = db('member') -> where("open_id", $openid) -> find();
//			if($user_info) {
//				session('user_info', $user_info);
//			}
//			session("openid",$openid);
//			if(!$user_info['nick_name']){
//				//$this -> redirect('index/guanzhu');
//			}
		}
		
	}
		/**
	 * 注册会员
	 */
	function up_user($openid){
		$appid=config('wxpay_appid');
		$secret=config('wxpay_appsercet');
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";	
		$res=file_get_contents($url);
		$jsondecode1 = json_decode($res); //对JSON格式的字符串进行编码
		$array1 = get_object_vars($jsondecode1);//转换成数组
		$access_token=$array1['access_token'];
		$res=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN");
		$jsondecode1 = json_decode($res); //对JSON格式的字符串进行编码
		$array1 = get_object_vars($jsondecode1);//转换成数组
		if(isset($array1['nickname'])){
			session('is_gz',1);
//			$data['nick_name']=$array1['nickname'];
//			$data['logo']=$array1['headimgurl'];
//			$data['is_atten']=1;
//			$user_id=db('member')->where("open_id", $openid)->update($data);
			//$user_info = db('member') -> where("open_id", $openid) -> find();
			//session('user_info', $user_info);
		}else{
			$this -> redirect('index/guanzhu');
		}
		
	}
	/**
	 * 注册会员
	 */
	function add_user($openid){
		$appid=config('wxpay_appid');
		$secret=config('wxpay_appsercet');
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";	
		$res=file_get_contents($url);
		$jsondecode1 = json_decode($res); //对JSON格式的字符串进行编码
		$array1 = get_object_vars($jsondecode1);//转换成数组
		$access_token=$array1['access_token'];
		$res=file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN");
		$jsondecode1 = json_decode($res); //对JSON格式的字符串进行编码
		$array1 = get_object_vars($jsondecode1);//转换成数组
		if(isset($array1['nickname'])){
			$data['nick_name']=$array1['nickname'];
			$data['logo']=$array1['headimgurl'];
			$data['is_atten']=1;
			session("is_gz",1);
		}else{
			$this -> redirect('index/guanzhu');
		}
		if(input("fromuid")){
			$data['pid1']=input("fromuid");
		}	
		$data['open_id']			= $openid;
		$data['add_time']		= now_time();
		$user_id=db('member')->insert($data);
		$user_info = db('member') -> where("open_id", $openid) -> find();
		session('user_info', $user_info);
	}
}

